// components/selectpro/selectpro.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    itemid:{  //通过itemid获取产品类型数据
      type:String,
      default:''
    },
    prosrc:{  //产品封面图
      type:String,
      default:''
    },
    ispay:{  //是否显示购买按钮
      type:String,
      default:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    /*  */
    showPop:true,
    /* 产品类型数据 */
    productTypes: [],
    /*  */
    proImg:'',  //产品图片
    finallyProName: '',  //选择的产品名称
    finallyProPriceOld:0, //原价
    finallyProPrice: 0, //选择的产品价格
    selectedProIndex: '0',  //选择的产品类型
    appletGrade:''
  },
  /*  */
  lifetimes:{
    created:function(){
    },
    attached:function(){
      //console.log(this.data.itemid);
      new Promise((resolve,reject)=>{
        let timer = null;
        clearInterval(timer);
        timer= setInterval(()=>{
          if(this.data.itemid!==''&&this.data.prosrc!==''&&this.data.ispay!==''){
            //console.log(this.data.itemid);
            clearInterval(timer);
            resolve();
          }
        },16);
      }).then(()=>{
        //从缓存获取用户信息
        this.setData({
          userInfo_share:app.globalData.get_userInfo()
        });
        console.log(this.data.userInfo_share);
      }).catch(err=>{

      });
    },
    ready:function(){
    }
  },
  /*  */
  pageLifetimes:{
    show:function(){
      
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /*  */
    /* 联系我们 */
    makePhoneContactUs:function(){
      wx.makePhoneCall({
        phoneNumber: ''+app.globalData.init_sale_phone,
      });
    },
    /* 获取用户手机号码 + 用户登录 */
    getPhoneNumber:function(e){
      return app.globalData.getPhoneNumber.call(this,e);
    },
    /* 获取用户信息 */
    getUserInfoFn: function (e) {
      app.globalData.getUserInfoFn.call(this,e);
    },
    /* 生成订单 */
    createOrder: function () {
      //
      let info = {
        pro_url: '',  //产品图片链接
        cost_price:'', //原价
        price: '', //金额
        product_id: '', //产品id
        titlte: '',  //支付标题
        appletGrade:'',  //产品类型
        pageFrom:'selectpro' //由selectpro跳转到订单详情
      };
      //
      let productData = this.data.productTypes[this.data.selectedProIndex * 1];
      console.log(productData);
      if (productData) {
        info.appletGrade = productData.appletGrade;
        info.pro_url = this.data.proImg;
        info.cost_price = productData.cost_price;
        info.price = productData.price;
        info.product_id = productData.product_id;
        info.titlte = productData.name;
      } else {
        wx.showToast({
          icon: 'none',
          title: '暂无对应的产品信息...'
        });
        return;
      }
      //return;
      wx.navigateTo({
        url: '/pages/orderModify/orderModify',
        success: res => {
          res.eventChannel.emit('fromProductPageForOrder', {
            info: info
          });
        }
      })
    },
    /*  */
    closeBuyProductPop: function () {
      this.setData({
        showPop: true
      })
    },
    /* 选择产品类型 */
    selectProType: function (e) {
      //console.log(e.currentTarget.dataset)
      let { index, info } = e.currentTarget.dataset;
      //
      //console.log(info);
      this.setData({
        proImg:info.img,
        selectedProIndex: index,
        finallyProName: info.name,
        finallyProPriceOld:app.globalData.showPrice(info.cost_price),
        finallyProPrice: app.globalData.showPrice(info.price)
      });
    },
    /*  */
    openBuyProductPop: function () {
      if(!this.data.userInfo_share.mobile){
        return;
      }
      //
      wx.showLoading({
        title: '获取产品信息',
        mask: true
      });
      //获取产品信息
      this.getProductsForItemId().then(() => {
        //
        this.setData({
          showPop:false,
          selectedProIndex: '0',
          proImg:this.data.productTypes[0].img,
          finallyProName: this.data.productTypes[0].name,
          finallyProPriceOld:app.globalData.showPrice(this.data.productTypes[0].cost_price),
          finallyProPrice: app.globalData.showPrice(this.data.productTypes[0].price)
        });
        //
        wx.hideLoading();
      }).catch(() => {
        wx.hideLoading();
      }).finally(() => {
        wx.hideLoading();
      });
    },
    /* 获取产品信息 */
    getProductsForItemId:function(){
      return app.globalData.$http.call(this,{
        api:"/getProductsForItemId",
        data:{
          item_id:this.data.itemid
        }
      }).then(res=>{
        console.log(res)
        if(res.data.code=='1'){
          //
          res.data.body.forEach((item,i)=>{
            if(i==0){
              item.checked = true;
            }else{
              item.checked = false;
            }
          });
          //
          this.setData({
            productTypes:res.data.body
          })
          return Promise.resolve()
        }else{
          wx.showToast({
            icon:'none',
            title:'获取产品信息失败'
          });
          return Promise.reject('获取产品信息失败')
        }
      }).catch(err=>{
        return Promise.reject('获取产品信息失败')
      })
    },
  }
})
